﻿using System;
using System.ComponentModel.Composition;
using System.Windows.Input;
using IntoNails.Infrastructure;
using IntoNails.Infrastructure.InteractionRequests;
using IntoNails.Models;
using IntoNails.Modules.CategoryManagement.Controllers;
using IntoNails.Modules.CategoryManagement.EventAggregators;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;

namespace IntoNails.Modules.CategoryManagement.ViewModels
{
    [Export(typeof(ICategoryRibbonTabViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class CategoryRibbonTabViewModel : NotificationObject, ICategoryRibbonTabViewModel
    {
        #region Fields

        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager _regionManager;

        private bool _isTabSelected;

        #endregion

        [ImportingConstructor]
        public CategoryRibbonTabViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
        {
            this._eventAggregator = eventAggregator;
            this._regionManager = regionManager;
            
            this.CreateNewCategoryCommand = new DelegateCommand(this.OnCreateNewCategoryCommandExecuted);
            this.RenameCategoryCommand = new DelegateCommand(this.OnRenameCategoryCommandExecuted);
            this.DeleteCategoryCommand = new DelegateCommand(this.OnDeleteCategoryCommandExecuted);

            this.CreateNewServiceCommand = new DelegateCommand(this.OnCreateNewServiceCommandExecuted);
            this.RenameServiceCommand = new DelegateCommand(this.OnRenameServiceCommandExecuted);
            this.DeleteServiceCommand = new DelegateCommand(this.OnDeleteServiceCommandExecuted);
        }

        #region Implementation of IHeaderInfoProvider<string>

        public string HeaderInfo
        {
            get { return "Category"; }
        }

        #endregion

        #region Properties

        public bool IsTabSelected
        {
            get { return this._isTabSelected; }
            set
            {
                this._isTabSelected = value;

                if (value)
                {
                    Uri workspaceUri = new Uri("CategoryWorkspaceView", UriKind.Relative);
                    this._regionManager.RequestNavigate(RegionNames.WorkspaceRegion, workspaceUri);
                }

                base.RaisePropertyChanged(() => IsTabSelected);
            }
        }

        #endregion

        #region Implementation of ICategoryRibbonTabViewModel
        
        public ICommand CreateNewCategoryCommand { get; private set; }
        public ICommand RenameCategoryCommand { get; private set; }
        public ICommand DeleteCategoryCommand { get; private set; }

        public ICommand CreateNewServiceCommand { get; private set; }
        public ICommand RenameServiceCommand { get; private set; }
        public ICommand DeleteServiceCommand { get; private set; }
        
        #endregion

        #region Command Action Handlers

        #region Category

        private void OnCreateNewCategoryCommandExecuted()
        {
            Category publishedCategory = Category.CreateCategory(string.Empty);

            this._eventAggregator.GetEvent<CreateNewCategoryEventAggregator>().Publish(publishedCategory);
        }

        private void OnRenameCategoryCommandExecuted()
        {
            this._eventAggregator.GetEvent<RenameCategoryEventAggregator>().Publish(string.Empty);
        }

        private void OnDeleteCategoryCommandExecuted()
        {
            MessageBoxContext context = new MessageBoxContext("Delete this category?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcons.Question);

            this._eventAggregator.GetEvent<DeleteCategoryEventAggregator>().Publish(context);
        }

        #endregion

        #region Service

        private void OnCreateNewServiceCommandExecuted()
        {
            Service publishedCategory = Service.CreateNewService();

            this._eventAggregator.GetEvent<CreateNewServiceEventAggregator>().Publish(publishedCategory);
        }

        private void OnRenameServiceCommandExecuted()
        {
            this._eventAggregator.GetEvent<EditServiceEventAggregator>().Publish(string.Empty);
        }

        private void OnDeleteServiceCommandExecuted()
        {
            MessageBoxContext context = new MessageBoxContext("Delete this service?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcons.Question);

            this._eventAggregator.GetEvent<DeleteServiceEventAggregator>().Publish(context);
        }

        #endregion

        #endregion
    }
}
